package com.tobeface.sns.interfaces;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kissme.core.orm.Page;
import com.kissme.core.web.exception.MaliciousRequestException;
import com.tobeface.sns.application.blog.BlogChannelService;
import com.tobeface.sns.application.blog.BlogCommentService;
import com.tobeface.sns.application.blog.BlogService;
import com.tobeface.sns.domain.blog.Blog;
import com.tobeface.sns.domain.blog.Blog.Visibility;
import com.tobeface.sns.domain.blog.BlogChannel;
import com.tobeface.sns.domain.blog.BlogComment;
import com.tobeface.sns.interfaces.util.UserUtil;

/**
 * @author Linxiaosheng
 */
@Controller
public class BlogController {
 
	@Autowired
	private BlogService blogService;
	
	@Autowired
	private BlogChannelService blogChannelService;
	
	@Autowired
	private BlogCommentService blogCommentService;
	
	private static final String REDIRECT_LIST = "redirect:/blog/list";
	
	@RequestMapping(value = "/blog/form", method = RequestMethod.GET)
	public String form(Model model) {
		
		Collection<BlogChannel> blogChannels = blogChannelService.findAll();
		model.addAttribute(new Blog().setBlogChannels(blogChannels))
			 .addAttribute("visibilitys", Arrays.asList(Visibility.values())); 
		
		return "/blog/form"; 
	}
	
	@RequestMapping(value = "/blog/form", method = RequestMethod.POST)
	public String form(Blog blog) {
		
		blogService.save(blog.setMemberId(UserUtil.getCurrentLoginUser().getMember().getId()));  
		return REDIRECT_LIST;
	}
	
	@RequestMapping(value = "/blog/list", method = RequestMethod.GET)
	public String list(Model model, Page<Blog> page) {
		
		page = blogService.findPage(page);
		model.addAttribute("page", page);
		return "/blog/list";
	}
	
	@RequestMapping(value = "/blog/blog/{id}", method = RequestMethod.GET)
	public String blog(Model model, @PathVariable("id")String id) {
		
		Blog blog = blogService.findOne(id);
		List<BlogComment> blogComments = blogCommentService.findCommentByBlogId(blog.getId());
		model.addAttribute("blog", blog)
			 .addAttribute("comments", blogComments); 
		return "/blog/blog";
	}
	
	@RequestMapping(value = "/blog/channel", method = RequestMethod.GET)
	public String channel(Model model) {
		
		model.addAttribute("blogChannel", new BlogChannel());
		return "/blog/channel";
	}
	
	@RequestMapping(value = "/blog/channel", method = RequestMethod.POST)
	public String chanle(@Valid BlogChannel blogChannel, BindingResult bindingResult) {
		
		if (bindingResult.hasErrors()) {
			throw new MaliciousRequestException("Bad request!");
		}
		
		blogChannelService.save(blogChannel);
		return REDIRECT_LIST;
	}
	
}
